<?php
/**********************************************************************//**\file
	Armory Player Data

	Description:  Player character data model, provides a consistent interface
		 to player character information pulled from wowarmory.com
*******************************************************************************/

	class playerdata
	{
		# variable declarations for each piece of player data, so it can be
		# retrieved via object reference
		public $data = array(
			'id' => null,
			'name' => null,
			'added' => null,
			'updated' => null,
			'expired' => null,
			'level' => null,
			'class' => null,
			'rank' => null,
			'race' => null,
			'gender' => null,
			'url' => null,
			'talents1' => null,
			'talents2' => null,
			'talents3' => null,
			'talents4' => null,
			'talents5' => null,
			'talents6' => null,
			'profname1' => null,
			'profskill1' => null,
			'profname2' => null,
			'profskill2' => null,
			'gearscore' => null,
			'geardate' => null,
			'gearsuggest' => null
		);

		private $query; #debug info

		# Flag true ONLY if the profile is modified via updater script, sets
		# "player_updated" time in query.  Used to check for expired data on the
		# next fresh pull from the armory.
		protected $xml_update = false;

		public function __construct($player = null, $xml_update = false)
		{
			$this->xml_update = $xml_update;

			if(is_int($player))
			{
				# Player specified by record id
				$this->get_player_id($player);
			}
			elseif(is_string($player))
			{
				# Player specified by name
				$this->get_player_name($player);
			}
			elseif(is_null($player))
			{
				# This condition would be used to add a player profile via some
				# other interface.  This is currently not used for anything.
			}
			else
			{
				# The data provided was invalid, kill the script.
				die();
			}
		}

		# pull player's data from db by id
		protected function get_player_id($id)
		{
			# pass id to get_player_data
			$this->get_player_data('id', $id);
		}

		# pull player's data from db by name
		protected function get_player_name($name)
		{
			# pass name to get_player_date
			$this->get_player_data('name', $name);
		}

		# helper function for other get_player functions
		protected function get_player_data($method, $value)
		{
			# build query based on id or name
			if($method == 'id')
			{
				$where = "WHERE player_id = ".$value;
			}
			elseif($method == 'name')
			{
				$where = "WHERE player_name = '".$value."'";
			}

			# query data, lazy selection
			$db = new armorydb();
			$db->query("SELECT * FROM armory_player $where LIMIT 1");
			$result_array = $db->result();

			# Assign values from database query to data object's array
			foreach($this->data as $key => $var)
			{
				$this->data[$key] = $result_array['player_'.$key];
			}

			# Close connection
			unset($db);
		}

		# UPDATE FUNCTIONS - Update the player's information
		public function update_general($class, $gender, $level, $race)
		{
			$this->data['class'] = $class;
			$this->data['gender'] = $gender;
			$this->data['level'] = $level;
			$this->data['race'] = $race;
		}

		public function update_talents($talents1, $talents2, $talents3)
		{
			if(!is_null($this->data['talents1']))
			{
				$this->data['talents4'] = $talents1;
				$this->data['talents5'] = $talents2;
				$this->data['talents6'] = $talents3;
			}
			else
			{
				$this->data['talents1'] = $talents1;
				$this->data['talents2'] = $talents2;
				$this->data['talents3'] = $talents3;
			}
		}

		public function update_professions($key, $value)
		{
			if(!is_null($this->data['profname1']))
			{
				$this->data['profname2'] = $key;
				$this->data['profskill2'] = $value;
			}
			else
			{
				$this->data['profname1'] = $key;
				$this->data['profskill1'] = $value;
			}
		}

		# Store data in the object to the db
		public function store()
		{
			$time = time();
			
			# If fresh data is being pulled from the armory, update timestamp
			# on the data so it will be pushed to the bottom of the update queue
			if($this->xml_update)
			{
				$updated = ', player_updated = '.$time.' ';
			}
			else
			{
				$update = '';
			}

			# Build SET data for query
			$set_data ='';
			$first = true;
			foreach($this->data as $key => $var)
			{
				if($key != 'id' && $key != 'name' && $key != 'updated' && !is_null($var))
				{
					if(!$first)
					{
						$set_data .= ',';
					}

					if(is_numeric($var) || $var == '0')
					{
						# If data is an integer, insert without quotes
						$set_data .= " player_$key = $var";
					}
					else
					{
						# If data is non-integer, insert with quotes
						$set_data .= " player_$key = '$var'";
					}
					
					$first = false;
				}
			}

			$db = new armorydb();

			# Check to see whether a record exists for this player
			$db->query("SELECT player_name FROM armory_player WHERE player_name = '{$this->data['name']}'");

			if(is_null($db->result()))
			{
				# Create a new record
				$this->query = "INSERT INTO armory_player SET player_name = '{$this->data['name']}', player_added = $time, $set_data $updated";
				$db->query($this->query);
			}
			else
			{
				# Update extant record
				$this->query = "UPDATE armory_player SET $set_data $updated WHERE player_name = '{$this->data['name']}'";
				$db->query($this->query);
			}

		}

		public function debug()
		{
			var_dump($this->query);
			var_dump($this->data);
		}
	}
?>
